﻿using Microsoft.EntityFrameworkCore;

namespace Devonline.Identity;

/// <summary>
/// 重载的用于工作安排的身份上下文, 提供字符串类型主键的默认实现
/// </summary>
public class WorkIdentityDbContext : WorkIdentityDbContext<User, Role, UserClaim, UserRole, UserLogin, RoleClaim, UserToken, Group, UserGroup, Level, Resource, AccessRule, AccessApply, AccessRecord, Parameter, Attachment, WorkStation, WorkShift, WorkSchedule, string>
{
    public WorkIdentityDbContext(DbContextOptions<WorkIdentityDbContext> options) : base(options) { }
}

/// <summary>
/// 重载的用于工作安排的身份上下文, 提供字符串类型主键的默认实现
/// </summary>
public class WorkIdentityDbContext<TKey> : WorkIdentityDbContext<User<TKey>, Role<TKey>, UserClaim<TKey>, UserRole<TKey>, UserLogin<TKey>, RoleClaim<TKey>, UserToken<TKey>, Group<TKey>, UserGroup<TKey>, Level<TKey>, Resource<TKey>, AccessRule<TKey>, AccessApply<TKey>, AccessRecord<TKey>, Parameter<TKey>, Attachment<TKey>, WorkStation<TKey>, WorkShift<TKey>, WorkSchedule<TKey>, TKey>
    where TKey : IEquatable<TKey>, IConvertible
{
    public WorkIdentityDbContext(DbContextOptions<WorkIdentityDbContext<TKey>> options) : base(options) { }
}

/// <summary>
/// 用于工作安排的身份上下文
/// </summary>
/// <typeparam name="TUser"></typeparam>
/// <typeparam name="TRole"></typeparam>
/// <typeparam name="TUserClaim"></typeparam>
/// <typeparam name="TUserRole"></typeparam>
/// <typeparam name="TUserLogin"></typeparam>
/// <typeparam name="TRoleClaim"></typeparam>
/// <typeparam name="TUserToken"></typeparam>
/// <typeparam name="TGroup"></typeparam>
/// <typeparam name="TUserGroup"></typeparam>
/// <typeparam name="TLevel"></typeparam>
/// <typeparam name="TResource"></typeparam>
/// <typeparam name="TAccessRule"></typeparam>
/// <typeparam name="TAccessApply"></typeparam>
/// <typeparam name="TAccessRecord"></typeparam>
/// <typeparam name="TParameter"></typeparam>
/// <typeparam name="TAttachment"></typeparam>
/// <typeparam name="TWorkStation"></typeparam>
/// <typeparam name="TWorkShift"></typeparam>
/// <typeparam name="TWorkSchedule"></typeparam>
/// <typeparam name="TKey"></typeparam>
public class WorkIdentityDbContext<TUser, TRole, TUserClaim, TUserRole, TUserLogin, TRoleClaim, TUserToken, TGroup, TUserGroup, TLevel, TResource, TAccessRule, TAccessApply, TAccessRecord, TParameter, TAttachment, TWorkStation, TWorkShift, TWorkSchedule, TKey> :
    IdentityDbContext<TUser, TRole, TUserClaim, TUserRole, TUserLogin, TRoleClaim, TUserToken, TGroup, TUserGroup, TLevel, TResource, TAccessRule, TAccessApply, TAccessRecord, TParameter, TAttachment, TKey>
    where TUser : User<TKey>
    where TRole : Role<TKey>
    where TUserClaim : UserClaim<TKey>
    where TUserRole : UserRole<TKey>
    where TUserLogin : UserLogin<TKey>
    where TRoleClaim : RoleClaim<TKey>
    where TUserToken : UserToken<TKey>
    where TGroup : Group<TKey>
    where TUserGroup : UserGroup<TKey>
    where TLevel : Level<TKey>
    where TResource : Resource<TKey>
    where TAccessRule : AccessRule<TKey>
    where TAccessApply : AccessApply<TKey>
    where TAccessRecord : AccessRecord<TKey>
    where TParameter : Parameter<TKey>
    where TAttachment : Attachment<TKey>
    where TWorkStation : WorkStation<TKey>
    where TWorkShift : WorkShift<TKey>
    where TWorkSchedule : WorkSchedule<TKey>
    where TKey : IEquatable<TKey>, IConvertible
{
    public WorkIdentityDbContext(DbContextOptions options) : base(options) { }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

        builder.Entity<TWorkStation>(b =>
        {
            b.Property(x => x.State).HasDefaultValue(DataState.Available);
            b.Property(x => x.Type).HasDefaultValue(AuthorizeType.Internal);
        });

        builder.Entity<TWorkShift>(b =>
        {
            b.Property(x => x.State).HasDefaultValue(DataState.Available);
            b.Property(x => x.Type).HasDefaultValue(AuthorizeType.Internal);
        });

        builder.Entity<TWorkSchedule>(b =>
        {
            b.Property(x => x.State).HasDefaultValue(DataState.Available);
            b.Property(x => x.CyclePeriod).HasDefaultValue(TimeKind.Month);
        });
    }

    #region work identity core entities
    /// <summary>
    /// WorkStations
    /// </summary>
    public virtual DbSet<TWorkStation>? WorkStations { get; set; }
    /// <summary>
    /// WorkShifts
    /// </summary>
    public virtual DbSet<TWorkShift>? WorkShifts { get; set; }
    /// <summary>
    /// WorkSchedules
    /// </summary>
    public virtual DbSet<TWorkSchedule>? WorkSchedules { get; set; }
    #endregion
}